package cn.tedu.blogproject.controller;


import cn.tedu.blogproject.common.response.JsonResult;
import cn.tedu.blogproject.common.response.StatusCode;
import cn.tedu.blogproject.mapper.CommentMapper;
import cn.tedu.blogproject.pojo.dto.CommentDTO;
import cn.tedu.blogproject.pojo.entity.Comment;
import cn.tedu.blogproject.pojo.vo.CommentVO;
import cn.tedu.blogproject.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "评论管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation(value = "发布评论方法")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        log.debug("commentDTO: " + commentDTO);

        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        int res = commentMapper.insertComment(comment);
        log.debug("插入评论返回值" +res);
        return JsonResult.ok();
    }

    @ApiOperation(value = "获取评论列表")
    @GetMapping("selectByweiboId")
    @ApiImplicitParam(name = "id",value = "微博id",required = true , example = "205")
    public JsonResult selectByweiboId(Long id){
        if(id < 0){
            throw new IllegalArgumentException("参数不可小于0");
        }
        List<CommentVO> clist= commentMapper.selectByweiboId(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,clist);
    }
}
